package com.jackrain.nea.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.util.WebUtils;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.face.impl.UserImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * Created by Lucky dog on 2016/10/26.
 */
@RestController
@Slf4j
public class HelloController {

    @RequestMapping(path = "/p/cs/hello", method = RequestMethod.GET)
    public JSONObject doHello(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject jo = new JSONObject();
        if(request.getSession().getAttribute("user") != null){
            UserImpl userWeb = (UserImpl) request.getSession().getAttribute("user");
            jo.put("name",userWeb.getName());
            jo.put("truename",userWeb.getTruename());
            jo.put("ename",userWeb.getEname());
        }
        return jo;
    }



    @RequestMapping(path = "/p/cs/check", method = RequestMethod.GET)
    public JSONObject doCheck(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = new JSONObject();
        User user = (User)request.getSession().getAttribute("user");
        String password = WebUtils.encryptBasedDes(user.getName(), user.getPwdrand().getBytes());
        if(password.equals(user.getPasswordHash())){
            result.put("code",2);
        }else{
            result.put("code",0);
        }
        return result;
    }

    @ApiOperation(value = "长时Get(有授权)", notes = "长时Get(有授权)")
    @RequestMapping(path = "/p/cs/longTimeGet", method = RequestMethod.GET)
    public JSONObject longTimeGetWithAuth(HttpServletRequest request, Long seconds){
        JSONObject jo = new JSONObject();
        try {
            Thread.sleep(seconds* 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        jo.put("code",0);
        return jo;
    }

    @ApiOperation(value = "长时Get", notes = "长时Get")
    @RequestMapping(path = "/p/c/longTimeGet", method = RequestMethod.GET)
    public JSONObject longTimeGet(HttpServletRequest request, Long seconds){
        JSONObject jo = new JSONObject();
        try {
            Thread.sleep(seconds* 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        jo.put("code",0);
        return jo;
    }

//    @RequestMapping("/login")
//    public String doLogin(Model model) {
//        if(ShiroSessionUtil.isLogin()) {
//            return "/tpl/ctl/index";
//        }
//        model.addAttribute("type","tpl");
//        return "tpl/pub/login/login";
//    }

    /**
     * 临时测试，稍后删掉
     */
    @Autowired
    private PropertiesConf pconf;

    @ApiOperation(value = "查看配置")
    @RequestMapping(value = "p/cs/loadProperties",method = RequestMethod.GET)
    public String getProperties(@RequestParam("key") String key){
        return pconf.getProperty(key);
    }
    @ApiOperation(value = "查看当前路径")
    @RequestMapping(value = "p/cs/absolutePath",method = RequestMethod.GET)
    public String getAbsolutePath(){
        return new File("").getAbsolutePath();
    }
}